//
//  ViewController.m
//  XQChartDemo
//
//  Created by xq on 2024/6/15.
//

#import "ViewController.h"
#import <Toast/Toast.h>
#import <Masonry/Masonry.h>
#import "VCView.h"

@interface ViewController () <XQChartViewDelegate>

@property (nonatomic, strong) VCView *contentView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.contentView = [VCView new];
    [self.view addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_topMargin);
        make.left.right.bottom.equalTo(self.view);
    }];
    
//    self.contentView.backgroundColor = UIColor.redColor;
    
    self.contentView.chartView.delegate = self;
    self.contentView.chartView.xAxisVisible = YES;
    self.contentView.chartView.yAxisVisible = YES;
    [self.contentView.chartView drawChartModel];
    
    [self.contentView.xAddBtn addTarget:self action:@selector(respondsWithXAdd:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView.xRemoveBtn addTarget:self action:@selector(respondsWithXRemove:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView.xSlider addTarget:self action:@selector(respondsWithXSlider:) forControlEvents:UIControlEventValueChanged];
    
    [self.contentView.yAddBtn addTarget:self action:@selector(respondsWithYAdd:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView.yRemoveBtn addTarget:self action:@selector(respondsWithYRemove:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView.ySlider addTarget:self action:@selector(respondsWithYSlider:) forControlEvents:UIControlEventValueChanged];
    
    [self.contentView.pointAddBtn addTarget:self action:@selector(respondsWithPointAdd:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView.pointRemoveBtn addTarget:self action:@selector(respondsWithPointRemove:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.contentView.selectPreBtn addTarget:self action:@selector(respondsWithSelectPre:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView.selectNextBtn addTarget:self action:@selector(respondsWithSelectNext:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.contentView.okBtn addTarget:self action:@selector(respondsWithOK:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView.clearBtn addTarget:self action:@selector(respondsWithClear:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
}

#pragma mark - responds

- (void)respondsWithXAdd:(UIButton *)sender {
    self.contentView.xSlider.value += 1;
    [self.contentView.chartView changeSelectPointXY:CGPointMake(self.contentView.xSlider.value, self.contentView.ySlider.value)];
}

- (void)respondsWithXRemove:(UIButton *)sender {
    self.contentView.xSlider.value -= 1;
    [self.contentView.chartView changeSelectPointXY:CGPointMake(self.contentView.xSlider.value, self.contentView.ySlider.value)];
}

- (void)respondsWithXSlider:(UISlider *)sender {
    [self.contentView.chartView changeSelectPointXY:CGPointMake(self.contentView.xSlider.value, self.contentView.ySlider.value)];
}

- (void)respondsWithYAdd:(UIButton *)sender {
    self.contentView.ySlider.value += 1;
    [self.contentView.chartView changeSelectPointXY:CGPointMake(self.contentView.xSlider.value, self.contentView.ySlider.value)];
}

- (void)respondsWithYRemove:(UIButton *)sender {
    self.contentView.ySlider.value -= 1;
    [self.contentView.chartView changeSelectPointXY:CGPointMake(self.contentView.xSlider.value, self.contentView.ySlider.value)];
}

- (void)respondsWithYSlider:(UISlider *)sender {
    [self.contentView.chartView changeSelectPointXY:CGPointMake(self.contentView.xSlider.value, self.contentView.ySlider.value)];
}

- (void)respondsWithPointAdd:(UIButton *)sender {
    [self.contentView.chartView autoAddPoint];
}

- (void)respondsWithPointRemove:(UIButton *)sender {
    [self.contentView.chartView removeSelectPoint];
}

- (void)respondsWithSelectPre:(UIButton *)sender {
    [self.contentView.chartView selectPrePoint];
}

- (void)respondsWithSelectNext:(UIButton *)sender {
    [self.contentView.chartView selectNextPoint];
}

- (void)respondsWithOK:(UIButton *)sender {
    NSLog(@"当前xy: %@", self.contentView.chartView.points);
    // 获取 CGPoint 值
//    NSValue *v = self.contentView.chartView.points[0];
//    CGPoint p = v.CGPointValue;
}

- (void)respondsWithClear:(UIButton *)sender {
    [self.contentView.chartView refreshWithPoints:@[]];
}

#pragma mark - XQChartViewDelegate

/// 已加载view，很多操作要在加载完成之后再进行
- (void)xqChartViewDidFinishLoad:(XQChartView *)xqChartView {
    // 测试
    // 初始化完成后添加点
    [self.contentView.chartView refreshWithPoints:@[
        [NSValue valueWithCGPoint:CGPointMake(5, 30)],
        [NSValue valueWithCGPoint:CGPointMake(50.5, 99)],
        [NSValue valueWithCGPoint:CGPointMake(55.5, 19)],
        [NSValue valueWithCGPoint:CGPointMake(100, 50)],
    ] selectIndex:1];
}

/// 取消/选择 某个点
- (void)xqChartView:(XQChartView *)xqChartView index:(NSUInteger)index point:(CGPoint)point select:(BOOL)select {
    if (index < 0) {
        return;
    }
    
    NSLog(@"xqChartView select: %ld", index);
    
    self.contentView.xSlider.value = point.x;
    self.contentView.ySlider.value = point.y;
}

@end
